<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Teacher extends Model
{
    //
    use SoftDelete;

    //添加教师
    public function teacher_add($data)
    {
        $validate = new \app\common\validate\Teacher();
        if (!$validate -> scene('tea') -> check($data)){
            return $validate -> getError();
        }
        $info = $data['photo'] -> move(ROOT_PATH.'public'.DS.'uploads/teacher');
        if ($info){
            $data['photo'] = '/uploads/teacher/'.$info -> getSaveName();
        } else {
            return $data -> getError();
        }
        $result = $this -> save($data);
        if ($result){
            return 1;
        } else {
            return '添加失败！';
        }
    }

    //编辑教师
    public function teacher_edit($data)
    {
        $validate = new \app\common\validate\Teacher();
        if ($data['photos']){
            $data['photo'] = $data['photos'];
            if (!$validate -> scene('tea') -> check($data)){
                return $validate -> getError();
            }
            $info = $data['photo'] -> move(ROOT_PATH.'public'.DS.'uploads/teacher');
            if ($info){
                $data['photo'] = '/uploads/teacher/'.$info -> getSaveName();
            } else {
                return $data -> getError();
            }
        } else {
            if (!$validate -> scene('edit') -> check($data)){
                return $validate -> getError();
            }
        }
        $te = $this -> find($data['id']);
        $te -> photo = $data['photo'];
        $te -> tname = $data['tname'];
        $te -> position = $data['position'];
        $te -> sex = $data['sex'];
        $te -> region = $data['region'];
        $te -> recome = $data['recome'];
        $te -> profile = $data['profile'];
        $result = $te -> save();
        if ($result){
            return 1;
        } else {
            return '更新失败！';
        }
    }

    //添加专业教师
    public function teachers_add($data)
    {
        $validate = new \app\common\validate\Teacher();
        if (!$validate -> scene('teas') -> check($data)){
            return $validate -> getError();
        }
        $info = $data['photo'] -> move(ROOT_PATH.'public'.DS.'uploads/teachers');
        if ($info){
            $data['photo'] = '/uploads/teachers/'.$info -> getSaveName();
        } else {
            return $data -> getError();
        }
        $result = $this -> save($data);
        if ($result){
            return 1;
        } else {
            return '添加失败！';
        }
    }

    //编辑专业教师
    public function teachers_edit($data)
    {
        $validate = new \app\common\validate\Teacher();
        if ($data['photos']){
            $data['photo'] = $data['photos'];
            if (!$validate -> scene('teas') -> check($data)){
                return $validate -> getError();
            }
            $info = $data['photo'] -> move(ROOT_PATH.'public'.DS.'uploads/teachers');
            if ($info){
                $data['photo'] = '/uploads/teachers/'.$info -> getSaveName();
            } else {
                return $data -> getError();
            }
        } else {
            if (!$validate -> scene('edits') -> check($data)){
                return $validate -> getError();
            }
        }
        $te = $this -> find($data['id']);
        $te -> photo = $data['photo'];
        $te -> tname = $data['tname'];
        $te -> position = $data['position'];
        $te -> major = $data['major'];
        $result = $te -> save();
        if ($result){
            return 1;
        } else {
            return '更新失败！';
        }
    }

}
